<?php
/**
 * Class that operate on table 'targettype'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-05-22 23:18
 */
class TargettypeMySqlDAO implements TargettypeDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return TargettypeMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM targettype WHERE PKTargetType = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM targettype';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM targettype ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param targettype primary key
 	 */
	public function delete($PKTargetType){
		$sql = 'DELETE FROM targettype WHERE PKTargetType = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($PKTargetType);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param TargettypeMySql targettype
 	 */
	public function insert($targettype){
		$sql = 'INSERT INTO targettype (Key00, Key01, Key02, Description, Comment, Active, Visible) VALUES (?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($targettype->key00);
		$sqlQuery->set($targettype->key01);
		$sqlQuery->set($targettype->key02);
		$sqlQuery->set($targettype->description);
		$sqlQuery->set($targettype->comment);
		$sqlQuery->setNumber($targettype->active);
		$sqlQuery->setNumber($targettype->visible);

		$id = $this->executeInsert($sqlQuery);	
		$targettype->pKTargetType = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param TargettypeMySql targettype
 	 */
	public function update($targettype){
		$sql = 'UPDATE targettype SET Key00 = ?, Key01 = ?, Key02 = ?, Description = ?, Comment = ?, Active = ?, Visible = ? WHERE PKTargetType = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($targettype->key00);
		$sqlQuery->set($targettype->key01);
		$sqlQuery->set($targettype->key02);
		$sqlQuery->set($targettype->description);
		$sqlQuery->set($targettype->comment);
		$sqlQuery->setNumber($targettype->active);
		$sqlQuery->setNumber($targettype->visible);

		$sqlQuery->setNumber($targettype->pKTargetType);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM targettype';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByKey00($value){
		$sql = 'SELECT * FROM targettype WHERE Key00 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByKey01($value){
		$sql = 'SELECT * FROM targettype WHERE Key01 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByKey02($value){
		$sql = 'SELECT * FROM targettype WHERE Key02 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM targettype WHERE Description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByComment($value){
		$sql = 'SELECT * FROM targettype WHERE Comment = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActive($value){
		$sql = 'SELECT * FROM targettype WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisible($value){
		$sql = 'SELECT * FROM targettype WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByKey00($value){
		$sql = 'DELETE FROM targettype WHERE Key00 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByKey01($value){
		$sql = 'DELETE FROM targettype WHERE Key01 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByKey02($value){
		$sql = 'DELETE FROM targettype WHERE Key02 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM targettype WHERE Description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByComment($value){
		$sql = 'DELETE FROM targettype WHERE Comment = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActive($value){
		$sql = 'DELETE FROM targettype WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisible($value){
		$sql = 'DELETE FROM targettype WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return TargettypeMySql 
	 */
	protected function readRow($row){
		$targettype = new Targettype();
		
		$targettype->pKTargetType = $row['PKTargetType'];
		$targettype->key00 = $row['Key00'];
		$targettype->key01 = $row['Key01'];
		$targettype->key02 = $row['Key02'];
		$targettype->description = $row['Description'];
		$targettype->comment = $row['Comment'];
		$targettype->active = $row['Active'];
		$targettype->visible = $row['Visible'];

		return $targettype;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return TargettypeMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>